package de.rac.vaadin.framework.ui;
/*******************************************************************************
 * Language switch data<br>
 * @author Volodymyr Dobryvechir (dobrivecher@yahoo.com)
 * @version 0.2 31.05.2012  <br>
 ******************************************************************************/
import com.vditl.core.*;
import java.io.*;
import java.util.*;
public class LanguagePool implements Serializable {
	
	private static final long serialVersionUID = 1564867093825719471L;
		private boolean isLanguagePoolSupported;
	    private UniVector pool;
	    private String defaultLanguage;
	    
     public LanguagePool()
     {
    	 isLanguagePoolSupported = false;
    	 pool= new UniVector();
    	 defaultLanguage=null;
     }
   	 public void addLanguage(String code,String baseName,String banner,boolean active,int sortPosition)
   	 {
   		 LanguageItem languageItem = new LanguageItem(code,baseName,banner,active,sortPosition);
   		 pool.add(languageItem);
   		 isLanguagePoolSupported = true;
   	 }
   	 public static Locale getLocaleByCode(String code)
   	 {
   		 return new Locale(code);
   	 }
   	 public void finishAdding()
   	 {
   		pool.reSortFilter();
   	 }
   	 public boolean isLanguageFunctionSupported()
   	 {
   		 return isLanguagePoolSupported && pool!=null && pool.getSortedFilteredSize()>=2;
   	 }
   	 public int getActiveLanguageNumber()
   	 {
          return pool.getSortedFilteredSize();   		 
   	 }
   	 public String getLanguageCode(int index)
   	 {
   		 return ((LanguageItem)pool.getSortedFilteredSource().get(index)).getCode();
   	 }
   	 public String getLanguageName(int index)
   	 {
   		 return ((LanguageItem)pool.getSortedFilteredSource().get(index)).getBaseName();
   	 }
   	 public String getLanguageBanner(int index)
   	 {
   		 return ((LanguageItem)pool.getSortedFilteredSource().get(index)).getBanner();
   	 }
   	 public void setDefaultLanguage(String code)
   	 {
   		 defaultLanguage=code;
   	 }
   	 public String getDefaultLanguage()
   	 {
   		 if (defaultLanguage==null)
   		 {
   	   		 if (pool.getSortedFilteredSize()==0) return null;
   			 LanguageItem languageItem =(LanguageItem) pool.getSortedFilteredSource().get(0);
   			 if (languageItem==null) return null;
   			 defaultLanguage = languageItem.getCode();
   		 }
   		 return defaultLanguage;
   	 }
   	 public String getLanguageSelectionList()
   	 {
   		 StringBuilder s = new StringBuilder();
   		 int n = getActiveLanguageNumber();
   		 for(int i=0;i<n;i++)
   		 {
   			 if (i!=0) s.append(',');
   			 s.append(getLanguageCode(i));
   			 s.append("->");
   			 s.append(getLanguageName(i));
   		 }
   		 return s.toString();
   	 }
   	 public class LanguageItem implements UniSortedFilteredItem,Serializable
     {
    	 
		private static final long serialVersionUID = 2591098180645440026L;
		private String code,baseName,banner;
    	 private boolean active;
    	 private int hashcode;
    	 int sortPosition;
    	 int filteredPlace=-1;
    	 public LanguageItem(String code,String baseName,String banner,boolean active,int sortPosition)
    	 {
             this.code = code;
             this.baseName = baseName;
             this.banner = banner;
             this.active = active;
             this.hashcode = code.hashCode();
             this.sortPosition = sortPosition;
    	 }
    	 public String getCode() { return code; }
    	 public String getBaseName() { return baseName;}
    	 public String getBanner() { return banner;}
    	 public boolean isActive() { return active; }
    	 @Override
    	 public int hashCode()
    	 {
    		 return hashcode;
    	 }
    	 @Override
    	 public boolean equals(Object o)
    	 {
    		 if (o==null) return false;
    		 return o.toString().equals(code);
    	 }
    	 @Override
    	 public String toString() { return code; } //only code!!!!
		@Override
		public int compareTo(Object c) {
			if (c==null || !(c instanceof LanguageItem)) return 1;
			LanguageItem item = (LanguageItem)c;
 			return sortPosition - item.sortPosition;
		}
		@Override
		public int getFilteredPlace() {
			return filteredPlace;
		}
		@Override
		public boolean isFiltered() {
			return active;
		}
		@Override
		public void setFiltered(boolean wActive) {
			active = wActive;
		}
		@Override
		public void setFilteredPlace(int place) {
			filteredPlace = place;
		}
     }
}
